package com.ruoyi.core.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.core.domain.custombean.ActivationCodeDetailQueryResult;
import com.ruoyi.core.service.IActivationCodeDetailService;
import com.ruoyi.core.vo.ActivationCodeDetailUpdateOperationVO;
import com.ruoyi.core.vo.BatchActivationCodeUpdateOperationVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * core_detailController
 *
 * @author ruoyi
 * @date 2020-09-28
 */
@Api("激活码明细信息管理")
@RestController
@RequestMapping("/activation-codes")
public class ActivationCodeDetailController extends BaseController {
    @Autowired
    private IActivationCodeDetailService activationCodeDetailService;

    /**
     * 查询core_detail列表
     */

    @ApiOperation("激活码明细列表")
    @PreAuthorize("@ss.hasRole('admin')")
    @GetMapping("/{batchId}")
    public TableDataInfo list(@PathVariable("batchId") Long batchId) {
        startPage();
        List<ActivationCodeDetailQueryResult> list = activationCodeDetailService.findByBatchId(batchId);
        return getDataTable(list);
    }

    /**
     * 导出core_detail列表
     */
    @Log(title = "激活码管理", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasRole('admin')")
    @ApiOperation("激活码导出")
    @GetMapping("/{batchId}/export")
    public AjaxResult export(@PathVariable("batchId") Long batchId, @RequestParam(name = "ids", required = false) Long[] ids) {
        List<ActivationCodeDetailQueryResult> results;
        if (ids != null && ids.length != 0) {
            results = activationCodeDetailService.findByIdSet(ids);
        } else {
            results = activationCodeDetailService.findByBatchId(batchId);
        }
        ExcelUtil<ActivationCodeDetailQueryResult> util = new ExcelUtil<>(ActivationCodeDetailQueryResult.class);
        return util.exportExcel(results, "activation-codes");
    }

    //    /**
    //     * 获取core_detail详细信息
    //     */
    //    @PreAuthorize("@ss.hasPermi('activation-code:query')")
    //    @GetMapping(value = "/{id}")
    //    public AjaxResult getInfo(@PathVariable("id") Long id) {
    //        return AjaxResult.success(activationCodeDetailService.selectActivationCodeDetailById(id));
    //    }

    //    /**
    //     * 新增core_detail
    //     */
    //    @PreAuthorize("@ss.hasPermi('activation-code:add')")
    //    @Log(title = "core_detail", businessType = BusinessType.INSERT)
    //    @PostMapping
    //    public AjaxResult add(@RequestBody ActivationCodeDetail activationCodeDetail) {
    //        return toAjax(activationCodeDetailService.insertActivationCodeDetail(activationCodeDetail));
    //    }

    /**
     * 修改core_detail
     */
    @Log(title = "激活码管理", businessType = BusinessType.UPDATE)
    @ApiOperation("激活码激活&冻结")
    @PreAuthorize("@ss.hasRole('admin')")
    @PutMapping
    public AjaxResult edit(@RequestBody BatchActivationCodeUpdateOperationVO batchActivationCodeUpdateOperationVO) {
        return AjaxResult.success(activationCodeDetailService.batchEdit(batchActivationCodeUpdateOperationVO.getActivationCodeDetails()));
    }

    //    /**
    //     * 删除core_detail
    //     */
    //    @PreAuthorize("@ss.hasPermi('activation-code:remove')")
    //    @Log(title = "core_detail", businessType = BusinessType.DELETE)
    //    @DeleteMapping("/{ids}")
    //    public AjaxResult remove(@PathVariable Long[] ids) {
    //        return toAjax(activationCodeDetailService.deleteActivationCodeDetailByIds(ids));
    //    }
}
